package com.hbnu.client.handler;

import com.hbnu.message.Response;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import io.netty.channel.SimpleChannelInboundHandler;

import static com.hbnu.service.FileService.PROMISE;


public class ResponseHandler extends SimpleChannelInboundHandler<Response> {

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, Response response) throws Exception {
        //接收响应
        //System.out.println("接收响应"+response);
        ChannelPromise promise = channelHandlerContext.channel().attr(PROMISE).get();
        if(response.isOk()) {
            promise.setSuccess();
        }else {
            System.out.println("请求中断！");
            promise.setFailure(new RuntimeException("请求中断！"));  // 请求成功
        }
        channelHandlerContext.close();
       // System.out.println(channelHandlerContext+"响应handle关闭！");
    }
}
